/**
 * Created by Limit on 2017/8/31.
 */


var APP_NOT_SUBMITTED="/cykspark/app/img/not-submit.png";
var APP_SUBMITTED_SUCCESS="/cykspark/app/img/submit-success.png";
var APP_SUBMITTED_FAILED="/cykspark/app/img/submit-failed.png";

function initializeAppStatus(appId){
    queryAppStatus(appId,bindAppSubmitStatus);
    $("#app-update-btn").click(function () {
        queryAppStatus(appId,bindAppSubmitStatus);
    });


}

function queryAppStatus(appId,callback) {
    var url="/cykspark/manage/getAppStatus";
    $.ajax({
       url:url,
        dataType:'json',
        data:{
           appId:appId
        },
        success:function(data) {
            callback(appId,data);
        },
        error:function () {
            callback(appId,null);
        }
    });
}

function bindAppSubmitStatus(appId,data) {

    var $appSubmitBtn=$("#app-submit-btn");

    var $appKillBtn=$("#app-kill-btn");

    if(data===null){
        setSubmitStatus("未提交");
        setProcessStatus("未运行");
        $appSubmitBtn.removeAttr("disabled").click(function () {
           submitApp(appId);
        });
    }else if(data.success){
        setSubmitStatus("提交成功");
        if(data.driverState==="ERROR"){
            setProcessStatus("运行错误");
            $appSubmitBtn.removeAttr("disabled").css("cursor","");
        }else if(data.driverState==="RUNNING"){
            setProcessStatus("正在运行");
            $appSubmitBtn.addClass("tip");
            $appSubmitBtn.css("cursor","not-allowed");
            $appSubmitBtn.removeAttr('onclick');
        }
        else if(data.driverState==="FINISHED"){
            setProcessStatus("运行完成");
            $appSubmitBtn.addClass("tip");
            $appSubmitBtn.css("cursor","not-allowed");
            $appSubmitBtn.removeAttr('onclick');
        }else if(data.driverState==="FAILED"){
            setProcessStatus("运行失败");
            $appSubmitBtn.removeAttr("disabled").css("cursor","");
        }else {

        }
    }else {
        setSubmitStatus("未提交");
        setProcessStatus("未运行");
        $appSubmitBtn.removeAttr("disabled").click(function () {
            submitApp(appId);
        });
    }
    $appKillBtn.addClass("tip");
    $appKillBtn.css("cursor","not-allowed");
    $appKillBtn.removeAttr('onclick');
    $(".tip").mouseover(function(){
        var _this = $(this);
        _this.justToolsTip({
            animation:"flipIn",
            contents:"已禁止，不可用",
            gravity:'bottom'
        });
    });
}

function submitApp(appId) {
    var url="/cykspark/manage/startApp";
    $.ajax({
       url:url,
        dataType:'json',
        data:{
           appId:appId
        },
        success:function (data) {
            console.log(data);
            if(data.hasOwnProperty("success")){
                if(data.success){
                    setSubmitStatus("提交成功");
                } else {
                    setSubmitStatus("提交失败");
                }
            }else {
                setSubmitStatus("未提交");
            }
        },
        error:function () {
            alert("提交请求失败！");
        }
    });
}

function stopApp(appId) {
    var url="/cykspark/manage/stopApp";
    $.ajax({
        url:url,
        dataType:'json',
        data:{
            appId:appId
        },
        success:function (data) {
            console.log(data);
            if(data.success){
                setProcessStatus("未运行");
            }else {
                alert("APP终止失败！");
            }
        },
        error:function () {
            alert("APP终止失败！");
        }
    });
}

function setSubmitStatus(status) {
    $("#app-submit-status").val(status);
}
function setProcessStatus(status) {
    $("#app-process-status").val(status);
}

